package com.wondertek.poms.init.controller;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.wondertek.poms.common.utils.StringUtil;
import com.wondertek.poms.init.config.InitConstants;
import com.wondertek.poms.init.po.InitData;
import com.wondertek.poms.init.po.InitTime;
import com.wondertek.poms.init.service.IInitDataService;
import com.wondertek.poms.init.service.IInitTimeService;

import cn.hutool.core.date.DateUtil;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Component
public class ScheduledTask {
	@Autowired
	private IInitDataService initDataService;
	@Autowired
	private IInitTimeService initTimeService;

	@Scheduled(cron = InitConstants.TASK)
	public void initData() {
		InitConstants.CURR_NUM.set(0);
		InitConstants.ALL_NUM = 0;
		if (InitConstants.OFF) {
			log.info("总开关关闭。");
			return;
		}
		InitConstants.ALL_NUM = notContinue();
		if (InitConstants.ALL_NUM == 0) {
			return;
		}
		Page<InitData> datas = initDataService.findAll(1, InitConstants.ALL_NUM, "asc", "id");
		
		for (InitData d : datas.getContent()) {
			if (InitConstants.CURR_NUM.get() >= InitConstants.ALL_NUM) {
				return;
			}
			
			initDataService.process(d);
		}
	}
	
	private int notContinue() {
		List<InitTime> its = initTimeService.findAll("desc", "id");
		if (its == null && its.size() <= 0) {
			log.info("配置表中没有时间段设置，默认给500个同步任务");
			return 500;
		}
		
		for (InitTime it : its) {
			boolean isOk = true;
			if (it.getYear() != null && DateUtil.year(new Date()) != it.getYear()) {
				log.info(it.getId() + " 不满足业务发送时间段 年：" + it.getYear());
				isOk = false;
			}
			
			if (isOk && StringUtil.isNotBlank(it.getMouth())) {
				boolean mouthOk = false;
				String[] mouths = it.getMouth().split(",");
				for (int i = 0; i < mouths.length; i++) {
					if (mouths[i].equals((DateUtil.month(new Date()) + 1) + "")) {
						log.info(it.getId() + " 满足业务发送时间段 月：" + it.getMouth());
						mouthOk = true;
						break;
					}
				}
				if (!mouthOk) {
					isOk = false;
					log.info(it.getId() + " 不满足业务发送时间段 月：" + it.getMouth());
				}
			}
			
			if (isOk && StringUtil.isNotBlank(it.getWeek())) {
				boolean weekOk = false;
				String[] weeks = it.getWeek().split(",");
				for (int i = 0; i < weeks.length; i++) {
					// 1.周日 2.周一 ... 7.周六
					if (weeks[i].equals(DateUtil.dayOfWeek(new Date()) + "")) {
						log.info(it.getId() + " 满足业务发送时间段 星期：" + it.getWeek());
						weekOk = true;
						break;
					}
				}
				if (!weekOk) {
					isOk = false;
					log.info(it.getId() + " 不满足业务发送时间段 星期：" + it.getWeek());
				}
			}
			
			if (isOk && StringUtil.isNotBlank(it.getHour())) {
				boolean hourOk = false;
				String[] hours = it.getHour().split(",");
				for (int i = 0; i < hours.length; i++) {
					if (hours[i].equals(DateUtil.hour(new Date(), true) + "")) {
						log.info(it.getId() + " 满足业务发送时间段 小时：" + it.getHour());
						hourOk = true;
						break;
					}
				}
				
				if (!hourOk) {
					isOk = false;
					log.info(it.getId() + " 不满足业务发送时间段 小时：" + it.getHour());
				}
			}
			
			if (isOk) {
				log.info(it.getId() + " 满足业务时间，对象为：" + it);
				return it.getNum();
			}
		}
		log.info("所有条件都不满足，不执行业务！");
		return 0;
	}

}
